package com.yunshan.member;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.yunshan.member.domain.MemberSubCardInfo;
import com.yunshan.member.service.IMemberSubCardInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunshan.common.annotation.Log;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.enums.BusinessType;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.common.utils.poi.ExcelUtil;
import com.yunshan.common.core.page.TableDataInfo;

/**
 * 会员信息Controller
 *
 * @author yunshan
 * @date 2025-01-17
 */
@Api(tags = "会员信息管理")
@RestController
@RequestMapping("/member/user")
public class MemberUserInfoController extends BaseController
{
    @Autowired
    private IMemberUserInfoService memberUserInfoService;
    @Autowired
    private IMemberSubCardInfoService memberSubCardInfoService;

    // 身份证号码正则表达式
    private static final String ID_CARD_REGEX = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
    // 手机号正则表达式
    private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";

    /**
     * 查询会员信息列表
     */
    @ApiOperation("查询会员信息列表")
    @PreAuthorize("@ss.hasPermi('member:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberUserInfo memberUserInfo)
    {
        startPage();
        List<MemberUserInfo> list = memberUserInfoService.selectMemberUserInfoList(memberUserInfo);
        return getDataTable(list);
    }

    /**
     * 查询会员信息列表（不分页）
     */
    @ApiOperation("查询会员信息列表不分页")
    @PreAuthorize("@ss.hasPermi('member:user:list')")
    @GetMapping("/query")
    public AjaxResult query(MemberUserInfo memberUserInfo)
    {
        List<MemberUserInfo> list = memberUserInfoService.selectMemberUserInfoList(memberUserInfo);
        return success(list);
    }

    /**
     * 导出会员信息列表
     */
    @ApiOperation("导出会员信息列表")
    @PreAuthorize("@ss.hasPermi('member:user:export')")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberUserInfo memberUserInfo)
    {
        List<MemberUserInfo> list = memberUserInfoService.selectMemberUserInfoList(memberUserInfo);
        ExcelUtil<MemberUserInfo> util = new ExcelUtil<MemberUserInfo>(MemberUserInfo.class);
        util.exportExcel(response, list, "会员信息数据");
    }

    /**
     * 获取会员信息详细信息
     */
    @ApiOperation("获取会员信息详细信息")
    @PreAuthorize("@ss.hasPermi('member:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(memberUserInfoService.selectMemberUserInfoById(id));
    }

    /**
     * 新增会员信息
     */
    @ApiOperation("新增会员信息")
    @PreAuthorize("@ss.hasPermi('member:user:add')")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberUserInfo memberUserInfo)
    {
        // 验证身份证号码
        if (memberUserInfo.getIdCard() != null && !memberUserInfo.getIdCard().matches(ID_CARD_REGEX)) {
            return AjaxResult.error("请输入正确的身份证号码");
        }
        // 验证身手机号
        if (memberUserInfo.getPhone() == null || memberUserInfo.getName() == null){
            return AjaxResult.error("请填写完整信息");
        }
        if (!memberUserInfo.getPhone().matches(PHONE_REGEX)) {
            return AjaxResult.error("请输入正确的电话");
        }

        //查询该手机号是否存在会员
        MemberUserInfo memberUser = memberUserInfoService.selectMemberUserInfoByLoginPhone(memberUserInfo.getPhone());
        if(memberUser != null && !"4".equals(memberUser.getMemberType())) {
            return AjaxResult.error("此手机号已注册会员");
        }
        MemberSubCardInfo memberSubCardInfo = memberSubCardInfoService.selectUserInfoByLoginPhone(memberUserInfo.getPhone());
        if (memberSubCardInfo != null && memberSubCardInfo.getStatus() != 3 && memberSubCardInfo.getStatus() != 2) {
            return AjaxResult.error("此手机号已为副卡会员使用");
        }
        return success(memberUserInfoService.insertMemberUserInfo(memberUserInfo));
    }

    /**
     * 修改会员信息
     */
    @ApiOperation("修改会员信息")
    @PreAuthorize("@ss.hasPermi('member:user:edit')")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberUserInfo memberUserInfo)
    {
        return toAjax(memberUserInfoService.updateMemberUserInfo(memberUserInfo));
    }

    /**
     * 删除会员信息
     */
    @ApiOperation("删除会员信息")
    @PreAuthorize("@ss.hasPermi('member:user:remove')")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberUserInfoService.deleteMemberUserInfoByIds(ids));
    }

    /**
     * 根据手机号获取用户信息
     */
    @ApiOperation("根据手机号获取用户信息")
    @PreAuthorize("@ss.hasPermi('member:user:query')")
    @GetMapping(value = "/queryByPhone")
    public AjaxResult getInfo(String phone) {
        try {
            MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoByPhone(phone);
            if (memberUserInfo != null) {
                return success(memberUserInfo);
            } else {
                MemberSubCardInfo memberSubCardInfo = memberSubCardInfoService.selectUserInfoByPhone(phone);
                memberUserInfo = memberUserInfoService.selectMemberUserInfoById(memberSubCardInfo.getMemberId());
                memberUserInfo.setId(memberSubCardInfo.getId());
                memberUserInfo.setName(memberSubCardInfo.getName());
                memberUserInfo.setPhone(memberSubCardInfo.getPhone());
                memberUserInfo.setMemberNum(null);
                return success(memberUserInfo);
            }
        }catch (Exception e){
            return success();
        }

    }
}
